package com.baidu.mpks.schema.service;


import com.baidu.mpks.schema.dao.UserSchemaClassDao;
import com.baidu.mpks.schema.dao.UserSchemaPropertyDao;
import com.baidu.mpks.schema.domain.UserSchema;
import com.baidu.mpks.schema.domain.UserSchemaClass;
import com.baidu.mpks.schema.domain.UserSchemaProperty;
import com.baidu.mpks.schema.dto.UserSchemaClassDto;
import com.baidu.mpks.schema.dto.UserSchemaPropertyDto;
import com.baidu.mpks.schema.dto.UserSchemaPropertyForWendaDto;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Service
public class UserSchemaServiceImpl implements UserSchemaService {
    private static final Logger log = LoggerFactory.getLogger(UserSchemaServiceImpl.class);
    @Autowired
    UserSchemaClassDao userSchemaClassDao;
    @Autowired
    UserSchemaPropertyDao userSchemaPropertyDao;

    public UserSchemaServiceImpl() {
    }

    private List<UserSchemaClassDto> setSchemaClassDtoList(List<UserSchemaClass> list) {
        List<UserSchemaClassDto> result = new ArrayList();
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            UserSchemaClass item = (UserSchemaClass)var3.next();
            UserSchemaClassDto classDto = this.setSchemaClassDto(item);
            result.add(classDto);
        }

        return result;
    }

    private UserSchemaClass setSchemaProperties(UserSchemaClass schemaClass) {
        List<UserSchemaPropertyDto> property = this.toSchemaPropertyDtoList(this.userSchemaPropertyDao.getByDomain(schemaClass.getAtId()));
        schemaClass.setProperties(new ArrayList());
        property.stream().forEach((item) -> {
            schemaClass.getProperties().add(item.getAtId());
        });
        return schemaClass;
    }

    private UserSchemaClassDto setSchemaClassDto(UserSchemaClass schemaClass) {
        UserSchemaClassDto classDto = new UserSchemaClassDto();
        List<String> subClassOf = new ArrayList();
        String subClassOfStr = schemaClass.getSubClass();
        if (StringUtils.isNotBlank(subClassOfStr)) {
            subClassOf.add(subClassOfStr);
        }

        classDto.setSubClassOf(subClassOf);
        UserSchemaClass schemaClassItem = schemaClass;

        ArrayList schemaClassAll;
        for(schemaClassAll = new ArrayList(); schemaClassItem != null; schemaClassItem = this.userSchemaClassDao.getOneByAtId(schemaClassItem.getSubClass())) {
            schemaClassAll.add(this.setSchemaProperties(schemaClassItem));
        }

        List<String> allProperties = new ArrayList();
        Iterator var8 = schemaClassAll.iterator();

        while(var8.hasNext()) {
            UserSchemaClass item = (UserSchemaClass)var8.next();
            allProperties.addAll(item.getProperties());
        }

        schemaClass.setProperties(allProperties);

        try {
            BeanUtils.copyProperties(classDto, schemaClass);
            List<UserSchemaClass> childrenList = this.userSchemaClassDao.getBySubClassOf(schemaClass.getAtId());
            List<String> subClassOfList = new ArrayList();
            Iterator var10 = childrenList.iterator();

            while(var10.hasNext()) {
                UserSchemaClass children = (UserSchemaClass)var10.next();
                subClassOfList.add(children.getAtId());
            }

            classDto.setSupClassOf(subClassOfList);
        } catch (InvocationTargetException | IllegalAccessException var12) {
            log.error(var12.getMessage());
        }

        return classDto;
    }

    public List<UserSchemaClassDto> getSchemaClassList() {
        List<UserSchemaClass> list = this.userSchemaClassDao.getAll();
        List<UserSchemaClassDto> result = this.setSchemaClassDtoList(list);
        return result;
    }

    public List<String> listClassNameByPidAndAtType(Integer pid, String atType) {
        if (pid == null) {
            log.info("pid is null");
            return Collections.EMPTY_LIST;
        } else {
            List<UserSchemaClass> schemaClasses = this.userSchemaClassDao.listByTypeAndPid(atType, pid);
            if (CollectionUtils.isEmpty(schemaClasses)) {
                log.info("schemaClasses is null pid------>{},atType---->{}", pid, atType);
                return Collections.EMPTY_LIST;
            } else {
                List<String> result = new ArrayList();
                Iterator var5 = schemaClasses.iterator();

                while(var5.hasNext()) {
                    UserSchemaClass schemaClass = (UserSchemaClass)var5.next();
                    String className = schemaClass.getAtId().replaceFirst("bkgs:", "");
                    className = className.replaceFirst("bkgss:", "");
                    if (!"事物".equals(className)) {
                        result.add(className);
                    }
                }

                return result;
            }
        }
    }

    public List<UserSchemaPropertyDto> getSelfSchemaPropertyList(String domain) {
        List<UserSchemaProperty> list = this.userSchemaPropertyDao.getByDomain(domain);
        List<UserSchemaPropertyDto> propDtoList = this.toSchemaPropertyDtoList(list);
        return propDtoList;
    }

    public List<UserSchemaPropertyDto> getInheritSchemaPropertyList(String domain) {
        List<UserSchemaPropertyDto> list = new ArrayList();

        String parentClassName;
        for(UserSchemaClass parentClass = this.userSchemaClassDao.getOneByAtId(domain); parentClass != null; parentClass = this.userSchemaClassDao.getOneByAtId(parentClassName)) {
            Integer classId = parentClass.getId();
            String className = parentClass.getAtId();
            className = "[\"" + className + "\"]";
            UserSchemaClassDto schemaClassDto = this.getSchemaInfo(classId);
            List<UserSchemaProperty> propList = this.userSchemaPropertyDao.getByDomain(className);
            UserSchemaPropertyDto propDto = new UserSchemaPropertyDto();
            List<UserSchemaPropertyDto> propDtoList = this.toSchemaPropertyDtoList(propList);
            propDto.setInheritProperties(propDtoList);

            try {
                BeanUtils.copyProperties(propDto, schemaClassDto);
            } catch (InvocationTargetException | IllegalAccessException var11) {
                log.error(var11.getMessage());
            }

            list.add(propDto);
            parentClassName = parentClass.getSubClass();
            parentClassName = parentClassName.replace("[\"", "");
            parentClassName = parentClassName.replace("\"]", "");
        }

        return list;
    }

    public List<UserSchemaPropertyDto> getInheritSchemaPropertyListMeta(String domain) {
        List<UserSchemaPropertyDto> list = new ArrayList();
        UserSchemaClass parentClass = this.userSchemaClassDao.getOneByAtId(domain);

        do {
            String className = parentClass.getAtId();
            className = "[\"" + className + "\"]";
            List<UserSchemaProperty> propList = this.userSchemaPropertyDao.getByDomain(className);
            UserSchemaPropertyDto propDto = new UserSchemaPropertyDto();
            List<UserSchemaPropertyDto> propDtoList = this.toSchemaPropertyDtoList(propList);
            propDto.setInheritProperties(propDtoList);
            list.add(propDto);
            String parentClassName = parentClass.getSubClass();
            parentClassName = parentClassName.replace("[\"", "");
            parentClassName = parentClassName.replace("\"]", "");
            parentClass = this.userSchemaClassDao.getOneByAtId(parentClassName);
        } while(parentClass != null);

        return list;
    }

    private List<UserSchemaPropertyDto> toSchemaPropertyDtoList(List<UserSchemaProperty> propList) {
        List<UserSchemaPropertyDto> propDtoList = new ArrayList();
        Iterator var3 = propList.iterator();

        while(var3.hasNext()) {
            UserSchemaProperty item = (UserSchemaProperty)var3.next();
            propDtoList.add(this.toSchemaPropertyDto(item));
        }

        return propDtoList;
    }

    private List<UserSchemaPropertyForWendaDto> toSchemaPropertyDtoListForWenda(List<UserSchemaProperty> propList) {
        List<UserSchemaPropertyForWendaDto> propDtoList = new ArrayList();
        Iterator var3 = propList.iterator();

        while(var3.hasNext()) {
            UserSchemaProperty item = (UserSchemaProperty)var3.next();
            propDtoList.add(this.toSchemaPropertyDtoForWenda(item));
        }

        return propDtoList;
    }

    private UserSchemaPropertyForWendaDto toSchemaPropertyDtoForWenda(UserSchemaProperty prop) {
        UserSchemaPropertyForWendaDto propDto = new UserSchemaPropertyForWendaDto();

        try {
            BeanUtils.copyProperties(propDto, prop);
            String enumData = prop.getExtend();
            String[] enumDataList = Strings.split(enumData, ',');
            propDto.setEnumData(Lists.newArrayList(enumDataList));
            Map<String, Object> constraint = new HashMap();
            constraint.put("numberOfValue", prop.getConstraint());
            propDto.setConstraintOfMap(constraint);
        } catch (InvocationTargetException | IllegalAccessException var6) {
            log.error(var6.getMessage());
        }

        return propDto;
    }

    private UserSchemaPropertyDto toSchemaPropertyDto(UserSchemaProperty prop) {
        UserSchemaPropertyDto propDto = new UserSchemaPropertyDto();

        try {
            BeanUtils.copyProperties(propDto, prop);
            String enumData = prop.getExtend();
            String[] enumDataList = Strings.split(enumData, ',');
            propDto.setEnumData(Lists.newArrayList(enumDataList));
        } catch (InvocationTargetException | IllegalAccessException var5) {
            log.error(var5.getMessage());
        }

        return propDto;
    }

    public List<UserSchema> getSchemaList() {
        List<UserSchemaPropertyForWendaDto> propList = this.toSchemaPropertyDtoListForWenda(this.userSchemaPropertyDao.getAll());
        List<UserSchemaClass> classList = this.userSchemaClassDao.getAll();
        List<UserSchemaClassDto> classDtoList = this.setSchemaClassDtoList(classList);
        List<UserSchema> list = new ArrayList();
        list.addAll(classDtoList);
        list.addAll(propList);
        return list;
    }

    public UserSchemaClassDto getSchemaInfo(Integer id) {
        UserSchemaClass schemaClass = this.userSchemaClassDao.getOneById(id);
        UserSchemaClassDto schemaClassDto = this.setSchemaClassDto(schemaClass);
        this.setSchemaProperties(schemaClass);
        return schemaClassDto;
    }
}